package sbc.corso.shareable;

import java.util.Vector;

import corso.lang.CorsoConnection;
import corso.lang.CorsoData;
import corso.lang.CorsoDataException;
import corso.lang.CorsoException;
import corso.lang.CorsoShareable;
import corso.lang.CorsoVarOid;

public class Platinen implements CorsoShareable {    
	
	private Vector platinenOids;

	
	public Platinen (CorsoConnection con) {
		platinenOids = new Vector();
	}	

	public Platinen (CorsoVarOid oid) {
	    //System.out.println ("Read PlatinenOid = " + oid);
	    try {
            oid.setTimeStamp (0);
            //System.out.println("Platinen");
	        oid.readShareable(this, null, CorsoConnection.INFINITE_TIMEOUT);
	    }
	    catch (CorsoException e) {
	        e.printStackTrace();
	    }
	}
	
	
	public void addPlatine(CorsoConnection con, Platine platine) {
		CorsoVarOid oid;
	    try {
            oid = con.createVarOid (con.getCurrentStrategy ());
            //System.out.println("addPlatine0");
            //System.out.println("platine.getFirstSensor().getName(): " + platine.getFirstSensorOid().getName());
            oid.writeShareable (platine, CorsoConnection.INFINITE_TIMEOUT);
            //System.out.println("addPlatine1");
            platinenOids.add(oid);
	    }
	    catch (CorsoException ce) {   
	        System.out.println (ce);
	        System.exit (-1);
	    }
	}

	public Vector getPlatinenOids() {
		return platinenOids;
	}


	public void read(CorsoData data) throws CorsoDataException {

        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);
        
        //System.out.println("arity: " + arity);
        
        

        platinenOids = new Vector ();
        for (int i = 0; i < arity; i++) {
            CorsoVarOid oid = new CorsoVarOid ();
            data.getShareable (oid);
            //System.out.println ("Naechste platinenOids = " + oid);
            platinenOids.add (oid);
        }
	}

	public void write(CorsoData data) throws CorsoDataException {
		data.putStructTag ("Platinen", platinenOids.size());
		//System.out.println("platineOids.size: " + platinenOids.size());
        for (int i = 0; i < platinenOids.size (); i++)	{
            data.putShareable ((CorsoVarOid) platinenOids.get (i));
            //System.out.println("platinenOids.get(i): " + platinenOids.get(i));
        }
        //System.out.println("Platinen written");
	}
}
